package com.lyf.emall.controller;

import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ProductList;
import com.lyf.emall.bean.ProductType;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.service.ProductService;
import com.lyf.emall.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * @Description:商品controller
 * @Author Lfy
 * @Date 2022/3/1-17:50
 */
@RestController
public class ProductController {

    @Autowired
    ProductService productService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    @GetMapping("/product/root")
    @PreAuthorize("hasAuthority('/product/root')")
    public ResponseResult getRootProductType(){
        return productService.getRootProductType();
    }

    @GetMapping("/product/children")
    @PreAuthorize("hasAuthority('/product/children')")
    public ResponseResult getChildrenProductType(@RequestParam String parentId){
        return productService.getChildrenProductType(Integer.valueOf(parentId));
    }

    @PostMapping("/product/root")
    @PreAuthorize("hasAuthority('/product/root')")
    public ResponseResult addRootProductType(@RequestParam String typeName,
                                             @RequestParam String remark){
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        ProductType productType = new ProductType(null, typeName, 1, 1, remark, timestamp, timestamp);
        return productService.addRootProductType(productType);
    }

    @PostMapping("/product")
    @PreAuthorize("hasAuthority('/product')")
    public ResponseResult addOtherProductType(@RequestParam String typeName,
                                              @RequestParam String parentId,
                                              @RequestParam String remark){
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        ProductType productType = new ProductType(null, typeName, 1, Integer.valueOf(parentId), remark, timestamp, timestamp);
        return productService.addOtherProductType(productType);
    }

    @PostMapping("/product/add")
    @PreAuthorize("hasAuthority('/product/add')")
    public ResponseResult addProduct(String productName,
                                     String typeId,
                                     String sellPoint,
                                     String productPrice,
                                     String productNum,
                                     String limitNum,
                                     MultipartFile productImage){
        // 先判断是否需要文件上传
        String upload = null;  // 文件名
        if (productImage != null){
            upload = fileUploadUtil.upload(productImage);
        }
        BigDecimal price = new BigDecimal(productPrice);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        ProductList productList = new ProductList(null, productName, Integer.valueOf(typeId), sellPoint, price, Integer.valueOf(productNum), Integer.valueOf(limitNum), upload, 1, timestamp, timestamp);

        ResponseResult responseResult = productService.addProduct(productList);

        return responseResult;
    }

    @GetMapping("/product")
    @PreAuthorize("hasAuthority('/product/list')")
    public ResponseResult showProduct(Page page){
        return productService.showProduct(page);
    }

    @PutMapping("/product/status")
    @PreAuthorize("hasAuthority('/product/status')")
    public ResponseResult updateProductStatus(@RequestParam String productId,
                                              @RequestParam String status){
        return productService.updateProductStatus(Integer.valueOf(productId), Integer.valueOf(status));
    }

    @PutMapping("/product")
    @PreAuthorize("hasAuthority('/product/update')")
    public ResponseResult updateProduct(String productId,
                                        String productName,
                                        String typeId,
                                        String sellPoint,
                                        String productPrice,
                                        String productNum,
                                        String limitNum,
                                        MultipartFile productImage){
        String upload = null;
        if (productImage != null){
            upload = fileUploadUtil.upload(productImage);
        }

        BigDecimal bigDecimal = new BigDecimal(productPrice);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(!StringUtils.hasText(productName)){
            return  new ResponseResult(400, "商品名称不能为空");
        }
        // 商品数量
        Integer num = Integer.valueOf(productNum);
        if (num < 0){
            num = null;
        }

        Integer limit = Integer.valueOf(limitNum);
        if (limit < 0){
            limit = null;
        }

        ProductList productList
                = new ProductList(Integer.valueOf(productId), productName, Integer.valueOf(typeId), sellPoint, bigDecimal, num, limit, upload, null, null, timestamp);

        return productService.updateProduct(productList);
    }

    @GetMapping("/product/search")
    @PreAuthorize("hasAuthority('/product/search')")
    public ResponseResult getProductByProductName(@RequestParam String productName,
                                                  Page page){
        return productService.getProductByProductName(productName, page);
    }

    @GetMapping("/product/type")
    @PreAuthorize("hasAuthority('/product/type')")
    public ResponseResult getProductByTypeId(@RequestParam String typeId,
                                             Page page){
        return productService.getProductByTypeId(Integer.valueOf(typeId), page);
    }
}
